package htmodels

import (
	"github.com/astaxie/beego/orm"
	"choujiang/models"
)
func HTDevicesAdd(devices *models.Devices) error {
	o := orm.NewOrm()
	_, err := o.Insert(devices)
	return err
}

func HTDevicesGetDetail(devices *models.Devices) error {
	o := orm.NewOrm()
	return o.Read(devices)
}

func HTDevicesGetCount(search string, searchFiled string , DeviceStatus int, DeviceType int) (int64, error) {
	o := orm.NewOrm()
	m := models.Devices{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceStatus(), DeviceStatus).Filter(m.FN_DeviceType(), DeviceType).Filter(searchFiled + "__icontains", search).Count()
	return count, err
}

func HTDevicesGetList(devicess *[]models.Devices, fields []string , DeviceStatus int, DeviceType int, offset int64, limit int, search string, searchFiled string, orderby string) error {
	o := orm.NewOrm()
	m := models.Devices{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceStatus(), DeviceStatus).Filter(m.FN_DeviceType(), DeviceType).Filter(searchFiled + "__icontains", search).OrderBy(orderby).Offset(offset).Limit(limit).All(devicess, fields...)
	return err
}

func HTDevicesDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.Devices{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Delete()
	return count, err
}
func HTDevicesStatusChange(Ids []int64, status int) (int64, error) {
	o := orm.NewOrm()
	m := models.Devices{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Update(orm.Params{
		m.FN_DeviceStatus(): status,
	})
	return count, err
}
func HTDevicesUpdate(devices *models.Devices) error {
	o := orm.NewOrm()
	_, err := o.Update(devices)
	return err
}